package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Negocio2.Turma;

public class TurmaDAO {
	private Connection conexao;
	public TurmaDAO() throws SQLException{
		this.conexao = CriaConexao.getConexao();
	}
	public void adiciona(Turma turma)throws SQLException{
		
		//Prepara conex�o
		String sql = "insert into turmas(cod_disciplina,sala,horario)"+
		"values(?,?,?)";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		
		
		//Seta os valores
		stmt.setString(1, turma.getDisciplina().getCodigo());
        stmt.setString(2, turma.getSala());
		stmt.setString(3, turma.getHorario());
		
		
		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}
	
	public List<Turma> getLista() throws SQLException{
		String sql = "select * from turma";
		PreparedStatement stmt = this.conexao.prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		
		List<Turma> listaTurmas = new ArrayList<Turma>();
		
		while(rs.next()){
			Turma turma = new Turma();
			turma.setCodigo(rs.getString("codigo"));
			turma.setHorario(rs.getString("horario"));
			turma.setDisciplina(rs.getString("cod_disciplina"));
			listaTurmas.add(turma);
		}
		rs.close();
		stmt.close();
		return listaTurmas;
	}
	
	public void altera(Turma turma) throws SQLException{
		String sql = "update turmas set cod_disciplina=?, sala=?, horario=? where codigo=?";
		PreparedStatement stmt = conexao.prepareStatement(sql);	
		
		//Seta os valores
		stmt.setString(1, turma.getDisciplina().getCodigo());
		stmt.setString(2, turma.getSala());
		stmt.setString(2, turma.getHorario());
		stmt.setString(2, turma.getCodigo());
		
		
		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}
	
	public void remove(Turma turma) throws SQLException{
		String sql = "delete from turmas where cod_turma =?";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setString(1, turma.getCodigo());
		stmt.execute();
		stmt.close();
	}

}
